package com.suntront.lib_db.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;


import com.suntront.lib_db.config.MyDbConfig;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class FileUtils {

	public static String SDPATH = MyDbConfig.picPath;

	public static File saveBitmap(Bitmap bm, String picName) {
		File file=null;
		try {
			if (!isFileExist("")) {
				File tempf = createSDDir("");
			}
			File f = new File(SDPATH, picName + ".JPEG");
			file=f;
			if (f.exists()) {
				f.delete();
			}
			FileOutputStream out = new FileOutputStream(f);
			bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

	public static File createSDDir(String dirName) throws IOException {
		File dir = new File(SDPATH + dirName);
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {

			System.out.println("createSDDir:" + dir.getAbsolutePath());
			System.out.println("createSDDir:" + dir.mkdir());
		}
		return dir;
	}

	public static boolean isFileExist(String fileName) {
		File file = new File(SDPATH + fileName);
		file.isFile();
		return file.exists();
	}
	
	public static void delFile(String fileName){
		File file = new File(SDPATH + fileName);
		if(file.isFile()){
			file.delete();
        }
		file.exists();
	}

	public static void deleteDir() {
		File dir = new File(SDPATH);
		if (dir == null || !dir.exists() || !dir.isDirectory())
			return;
		
		for (File file : dir.listFiles()) {
			if (file.isFile())
				file.delete(); 
			else if (file.isDirectory())
				deleteDir(); 
		}
		dir.delete();
	}

	public static boolean fileIsExists(String path) {
		try {
			File f = new File(path);
			if (!f.exists()) {
				return false;
			}
		} catch (Exception e) {

			return false;
		}
		return true;
	}
	/**
	 * 通过图片的存储路径生成图片的base64码
	 * @param imgPath
	 * @return
	 */
	public static String imgToBase64(String imgPath) {

		ByteArrayOutputStream out = null;
		try {
			Bitmap bitmap = null;
			if (imgPath != null && imgPath.length() > 0) {
				bitmap = BitmapFactory.decodeFile(imgPath);
			}
			out = new ByteArrayOutputStream();
			if(bitmap!=null){
				bitmap = Bitmap.createScaledBitmap(bitmap,400,400,true);  //600+k
				bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			}

			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
				Log.e("bitmap-size", bitmap.getAllocationByteCount()/1024+"kb");
			}
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
				Log.e("bitmap-size", bitmap.getByteCount()/1024+"kb");
			}
			out.flush();
			out.close();
			bitmap.recycle();
			bitmap=null;
			byte[] imgBytes = out.toByteArray();
			return Base64.encodeToString(imgBytes, Base64.NO_WRAP);//此处的NO_WRAP为：略去所有的换行符
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				out.flush();
				out.close();
				out=null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


}
